import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import VueSetupExtend from 'vite-plugin-vue-setup-extend'
import { fileURLToPath, URL } from "node:url";
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
export default defineConfig(({ mode }) => {
  const viteEnv = loadEnv(mode, './')
  return {
    base: viteEnv.VITE_BASE,
    server: {
      host: "localhost",
      port: "3000",
      https: false,
      open: true,
      hmr: true,
      proxy: {
        [viteEnv.VITE_BASE_URL]: {
          target: viteEnv.VITE_BASE_SERVER_URL,
          changeOrigin: true,
          rewrite: (path) => path.replace(viteEnv.VITE_BASE_URL, "/"),
        },
      },
    },
    resolve: {
      alias: [
        {
          find: "@",
          replacement: fileURLToPath(new URL("./src", import.meta.url)),
        },
        {
          find: "@img",
          replacement: fileURLToPath(
            new URL("./src/assets/images", import.meta.url)
          ),
        },
        {
          find: "vue",
          replacement: "vue/dist/vue.esm-bundler.js",
        },
      ],
      extensions: [".ts", ".js"],
    },

    plugins: [
      vue({
        refTransform: true, // 开启ref转换
      }),
      VueSetupExtend(),
      AutoImport({
        imports: ["vue", "pinia", "vue-router", "@vueuse/core"],
      }),
      Components({
        dirs: ["src/components/", "src/view/"],
        include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
        resolvers: [],
      }),
    ],
  };
})